const modelSave = {
    mounted(el, binding) {
        const setInputValue = (value) => el.value = value;
        const setSyncDate = (value) => binding.instance[el.id] = value;
        const setLocalStorage = (value) => localStorage.setItem(`form-${el.id}`, value);

        // 檢查目前是 localStorage 有沒有暫存的資料
        if (!localStorage[`form-${el.id}`]) {
            setLocalStorage(binding.value);
        }

        setInputValue(localStorage[`form-${el.id}`]);
        setSyncDate(el.value);

        el.handSave = (e) => {
            setLocalStorage(e.target.value);
            setSyncDate(e.target.value);
        }

        el.addEventListener("input", el.handSave);
        el.addEventListener("change", el.handSave);
    },
    unmounted(el) {
        el.removeEventListener("input", el.handSave);
        el.removeEventListener("change", el.handSave);
    },
}

export default modelSave